package com.resou.reader.mine.coupon.fragment;

import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.resou.reader.R;
import com.resou.reader.api.ApiConstant;
import com.resou.reader.api.entry.CouponBean;
import com.resou.reader.assist.WebActivity;
import com.resou.reader.base.ResouBaseFragment;
import com.resou.reader.mine.IView.UserInstance;
import com.resou.reader.mine.adapter.CouponAdapter;
import com.resou.reader.utils.ToastUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.List;

import butterknife.BindView;


/**
 * 未使用过的优惠券页面
 */
public class Fragment_unused extends ResouBaseFragment<FragmentCouponPresenter> implements FragmentCouponContract.ICouponView {
    private static final String TAG = "show";
    public static Fragment_unused fragment;
    @BindView(R.id.mSmartRefreshLayout)
    SmartRefreshLayout mSmartRefreshLayout;
    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;
    CouponAdapter couponAdapter = null;
    int page = 0;

    public Fragment_unused() {
    }

    public static Fragment_unused newInstance() {
        if (fragment == null) {
            synchronized (Fragment_unused.class) {
                if (fragment == null) {
                    fragment = new Fragment_unused();
                }
            }
        }
        return fragment;
    }

    @Override
    protected void initPresenter() {
        mPresenter = new FragmentCouponPresenter(this);
    }

    @Override
    protected void initView() {
        couponAdapter = new CouponAdapter(R.layout.adapter_coupon_item, null);
        couponAdapter.openLoadAnimation(BaseQuickAdapter.SCALEIN);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mRecyclerView.setAdapter(couponAdapter);
        mSmartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 0;
                couponAdapter.getData().clear();
                mPresenter.quearCouponData(page, 0);
            }
        });
        mSmartRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                mPresenter.quearCouponData(page++, 0);
            }
        });
    }

    @Override
    protected void retryRefreshData() {
        page = 0;
        couponAdapter.getData().clear();
        mPresenter.quearCouponData(page, 0);
    }


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_unused;
    }

    @Override
    public void showCouponData(List<CouponBean> dataList) {
        mSmartRefreshLayout.finishRefresh(true);
        couponAdapter.addCoupontData(dataList, 0);
    }


    @Override
    public void initData() {
        page = 0;
        couponAdapter.getData().clear();
        mPresenter.quearCouponData(page, 0);
    }

    @Override
    public void showContent() {
        Log.i(TAG, "showContent: ");
    }

    @Override
    public void showProgress() {
        Log.i(TAG, "showProgress: ");
    }

    @Override
    public void showError(Throwable throwable) {
        ToastUtil.makeShortToast("网络异常");
        showNetErrorView(R.id.rlContent);
    }

    @Override
    public SmartRefreshLayout getmSmartRefreshLayout() {
        return mSmartRefreshLayout;
    }


    @Override
    public void showEmpty() {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.layout_nulldata, null);
        couponAdapter.setEmptyView(view);
        TextView tvGoCoupon = view.findViewById(R.id.tvGoCoupon);
        tvGoCoupon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                WebActivity.startActivity(getActivity(), ApiConstant.VIPDUIBA + UserInstance.getToken(), "签到");

            }
        });
    }

}
